'use strict';

const mongoose = require('mongoose'),
    deliverInfoModel = mongoose.model('deliverInfo');

module.exports = {
    addDeliverInfoByClient:addDeliverInfoByClient,
    getDeliverInfoByClient:getDeliverInfoByClient,
    updateDeliverInfo:updateDeliverInfo,
    deleteDeliverInfoById:deleteDeliverInfoById
}

function addDeliverInfoByClient(deliverInfo, callback) {
    deliverInfoModel.create(deliverInfo, function(error, data) {
        callback(null, data);
    })
}

function getDeliverInfoByClient(cid, callback) {
    deliverInfoModel.find({'client._id':cid}, function(error, data) {
        callback(null, data);
    })
}

function updateDeliverInfo(deliverInfo, callback) {
    deliverInfoModel.update({'_id':deliverInfo._id},
                {$set:{
                    'contact':deliverInfo.contact,
                    'tel':deliverInfo.tel,
                    'addr':deliverInfo.addr
                }},
                function(error, data) {
                    deliverInfoModel.findOne({'_id':deliverInfo._id}, function(error, data) {
                        callback(null, data);
                    })
                })
}

function deleteDeliverInfoById(did, callback) {
    deliverInfoModel.deleteOne({'_id':did}, function(error, data) {
                    callback(null, data);
                });
}

